package com.ylxl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylxl.common.constant.UserConstants;
import com.ylxl.common.domain.SysUser;
import com.ylxl.common.exception.BaseException;
import com.ylxl.common.utils.SecurityUtils;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.*;
import com.ylxl.entity.request.*;
import com.ylxl.mapper.LabelMapper;
import com.ylxl.mapper.SysUserMapper;
import com.ylxl.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户 业务层处理
 *
 */
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label>  implements ILabelService {
    private static final Logger log = LoggerFactory.getLogger(LabelServiceImpl.class);
    @Resource
    private LabelMapper labelMapper;

    @Override
    public List<Label> selectLabelList(QueryLabel queryLabel) {
        LambdaQueryWrapper<Label> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        return labelMapper.selectList(labelLambdaQueryWrapper);
    }

    @Override
    public boolean checkNameUnique(LabelCmdOrMdy labelCmdOrMdy) {
        if(StringUtils.isEmpty(labelCmdOrMdy.getLabelName())){
            throw new BaseException("标签名称不能为空");
        }
        LambdaQueryWrapper<Label> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(Label::getLabelName,labelCmdOrMdy.getLabelName())
                .ne(StringUtils.isNotEmpty(labelCmdOrMdy.getId()), Label::getId,labelCmdOrMdy.getId());
        List<Label> labels = labelMapper.selectList(labelLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(labels)){
            return UserConstants.UNIQUE;
        }
        return UserConstants.NOT_UNIQUE;
    }

    @Override
    public int insertLabel(LabelCmdOrMdy labelCmdOrMdy) {
        Label labelInsert = new Label();
        BeanUtil.copyProperties(labelCmdOrMdy, labelInsert);//属性拷贝
        return labelMapper.insert(labelInsert);
    }

    @Override
    public int updateLabel(LabelCmdOrMdy labelCmdOrMdy) {
        if(StringUtils.isEmpty(labelCmdOrMdy.getId())){
            throw new BaseException("标签id不能为空");
        }
        Label labelUpdate = new Label();
        BeanUtil.copyProperties(labelCmdOrMdy, labelUpdate);//属性拷贝
        return labelMapper.updateById(labelUpdate);
    }

    @Override
    public Label selctById(Integer id) {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("标签id不能为空");
        }
        return labelMapper.selectById(id);
    }

    @Override
    public int deleteById(Integer id) {
        if(StringUtils.isEmpty(id)){
            throw new BaseException("标签id不能为空");
        }
        return labelMapper.deleteById(id);
    }
}
